#Cairo canvas. The following variables are available:
# * CNTX: A reference to the current cairo context object
# * W: Canvas width
# * H: Canvas height
# Have fun!

import math, cairo

def segment( dim, count ):
    if count > 3:
        CNTX.rel_line_to(dim, 0)
        return
    
    new_dim = dim / 3.0
    segment( new_dim, count + 1 )
    CNTX.rotate( -math.pi / 3 )
    segment( new_dim, count + 1 )
    CNTX.rotate( 2*math.pi / 3 )
    segment( dim / 3.0, count + 1 )
    CNTX.rotate( -math.pi / 3 )
    segment( dim / 3.0, count + 1 )
    pass

def snowflake():
    CNTX.move_to(0.0, 0.0)
    CNTX.rotate( -math.pi / 3 )
    segment(1, 1)
    CNTX.rotate( 2*math.pi / 3 )
    segment(1, 1)
    CNTX.rotate( 2*math.pi / 3 )
    segment(1, 1)
    CNTX.close_path()
    CNTX.stroke()
    pass
    
#CNTX.set_source_color(COLOR("black"))
CNTX.set_source_rgb(0, 0, 0)
CNTX.set_line_cap(cairo.LINE_CAP_ROUND )
size_factor = W / 2 
CNTX.set_line_width(1.0/W)
CNTX.translate(size_factor / 2, size_factor)
CNTX.scale(size_factor, size_factor)
snowflake()
